package com.et114.modules.myet.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.core.common.AppContext;
import com.et114.modules.cust.BIZ;
import com.et114.modules.cust.services.IMng_CustService;
import com.et114.modules.cust.services.Mng_CustServiceImp;
import com.et114.modules.cust.vo.CustBaseVo;
import com.et114.modules.cust.vo.CustInfoVo;
import com.et114.modules.myet.form.MyEtLgiFrom;
import com.et114.modules.myet.service.IMyEt_UCenterService;
import com.et114.modules.myet.service.MyEt_UCenterServiceImp;

/**
 * 我的订单
 * 
 * @author zhiweip
 * @version 1.0
 * @created 22-十二月-2010 13:33:56
 */
public class MyEt_InfoAction extends MyEtAction {
	
	private IMyEt_UCenterService	m_IMyEt_UCenterService	= ( MyEt_UCenterServiceImp ) getBean ( "m_IMyEt_UCenterService" );
	private IMng_CustService		mng_CustService			= ( Mng_CustServiceImp ) getBean ( "mng_CustService" );
	
	public MyEt_InfoAction ( ) {
		
	}
	
	public void finalize ( ) throws Throwable {
		
	}
	
	public ActionForward cret( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		if ( isNeedLogin ( request ) ) {
			response.sendRedirect ( "login.html" );return mapping.findForward ( "" );
		}
		
		CustInfoVo info = new CustInfoVo ( );
		info.setCustid ( getCustId ( request ) );
		info.setDtype ( BIZ.INFO_CERT );
		request.setAttribute ( "LgiList" , mng_CustService
				.loadCustMsgByType ( info ) );
		
		return mapping.findForward ( "queryList" );
	}
	public ActionForward lgi ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		if ( isNeedLogin ( request ) ) {
			response.sendRedirect ( "login.html" );return mapping.findForward ( "" );
		} 
		logger.debug ("================lgi====================");
		CustInfoVo info = new CustInfoVo ( );
		info.setCustid ( getCustId ( request ) );
		info.setDtype ( BIZ.INFO_LINK );
		request.setAttribute ( "LgiList" , mng_CustService
				.loadCustMsgByType ( info ) ); 
		return mapping.findForward ( "queryList" );
	}
	public ActionForward lic ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		if ( isNeedLogin ( request ) ) {
			response.sendRedirect ( "login.html" );return mapping.findForward ( "" );
		}  
		CustInfoVo info = new CustInfoVo ( );
		info.setCustid ( getCustId ( request ) );
		info.setDtype ( BIZ.INFO_LIC );
		request.setAttribute ( "LgiList" , mng_CustService
				.loadCustMsgByType ( info ) );
		
		return mapping.findForward ( "queryList" );
	}
	
	public ActionForward setDefault ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		if(request.getParameter ( "dtype" )==null) throw new Exception ("无效类型!");
		
		if ( isNeedLogin ( request ) ) {
			response.sendRedirect ( "login.html" );return mapping.findForward ( "" );
		}  
		String lid = request.getParameter ( "lid" );
		String dtype = request.getParameter ( "dtype" );
		
		CustInfoVo info = new CustInfoVo ( );
		info.setCustid ( getCustId ( request ) );
		info.setLid ( new Integer(lid) );
		info.setDefaultflag ( "1" );
		Map map = new HashMap ( );
		if(1==m_IMyEt_UCenterService.updateUserLgiDefault ( info )){
			map.put ( "opFlag" , "ok" );
		}else{
			map.put ( "opFlag" , "deny" ); 
		} 
		JSONArray comjson = JSONArray.fromObject ( map );
		String result = comjson.toString ( );
		response.getWriter ( ).print ( result );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}
	
	/**
	 * 
	 */
	public ActionForward update ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		logger.debug ("==============update==================");
		if ( isNeedLogin ( request ) ) {  response.sendRedirect ( "login.html" );return mapping.findForward ( "" ); }
		if(request.getParameter ( "dtype" )==null) throw new Exception ("无效类型!");
		
		MyEtLgiFrom lForm = ( MyEtLgiFrom ) form;
		String dtype=request.getParameter ( "dtype" );
		CustInfoVo info = new CustInfoVo ( );
		info.setCustid ( getCustId ( request ) );
		info.setDtype ( dtype );
		List list = mng_CustService.loadCustMsgByType ( info );
		
		if ( list.size ( ) >= 5 ) {
			request.setAttribute ( "opFlag" , "moreThanfive" );
		} else {
			PropertyUtils.copyProperties ( info , lForm );
			info.setCustid ( getCustId ( request ) );
			info.setDtype ( dtype ); 
			if(dtype.equals ( BIZ.INFO_CERT )&&info.getBillno ( ).indexOf ( "*" )>-1){
				info.setBillno ( null );
			}
			
			m_IMyEt_UCenterService.updateUserLgi ( info );
		}
		if(dtype.equals ( BIZ.INFO_LINK )){
			
			response.sendRedirect ( "MyEt_LgiInfoAction.go?method=lgi" );
		}else if(dtype.equals ( BIZ.INFO_CERT )){
			response.sendRedirect ( "MyEt_CretInfoAction.go?method=cret" );
		}if(dtype.equals ( BIZ.INFO_LIC )){
			response.sendRedirect ( "MyEt_LicInfoAction.go?method=lic" );
		}
		
		return mapping.findForward ( "queryList" );
	}
	
	public ActionForward getCounter ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		if(request.getParameter ( "dtype" )==null) throw new Exception ("无效类型!");
		
		CustInfoVo info = new CustInfoVo ( );
		info.setCustid ( getCustId ( request ) );
		info.setDtype ( request.getParameter ( "dtype" ) );
		info.setOperType ( "edit" );
		String lid=request.getParameter ( "lid" );
		String billno=request.getParameter ( "billno" );
		List<CustInfoVo> list = mng_CustService.loadCustMsgByType ( info );
		
		Map map = new HashMap ( );
		if ( list != null && list.size ( ) >= 5 ) {
			map.put ( "opFlag" , "deny" );
		} else{
			map.put ( "opFlag" , "ok" );
		}
		if(null!=lid&&lid.length ( )>0){
			map.put ( "opFlag" , "ok" );
		}
		if(null!=lid&&null!=billno){
			for(CustInfoVo cust:list){
				if(cust.getBillno ( ).equals ( billno )){
					if(!cust.getLid ( ).toString ( ).equals ( lid )){
						map.put ( "opFlag" , "rep" );
					}
				}
			}
		}
		//System.out.println ( "========================key=:" + map.get ( "opFlag" )+" lid:"+lid );
		JSONArray comjson = JSONArray.fromObject ( map );
		String result = comjson.toString ( );
		response.getWriter ( ).print ( result );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}
	
	public ActionForward del ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		if(request.getParameter ( "dtype" )==null) throw new Exception ("无效类型!");
		
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		
		Map map = new HashMap ( );
		map.put ( "opFlag" , "error" );
		if ( getCustId ( request ) != null ) {
			CustInfoVo info = new CustInfoVo ( );
			info.setCustid ( getCustId ( request ) );
			info.setDtype ( request.getParameter ( "dtype" ) );
			String lid = request.getParameter ( "lid" );
			map.put ( "opFlag" , "ok" );
			if ( lid != null ) {
				try {
					info.setLid ( new Integer ( lid ) );
					if ( mng_CustService.deleteCust_info_byKey ( info ) == 1 ) {
						map.put ( "opFlag" , "ok" );
					}
				} catch ( Exception e ) {
				}
				;
			}
		}
		JSONArray comjson = JSONArray.fromObject ( map );
		String result = comjson.toString ( );
		response.getWriter ( ).print ( result );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}
	
	public ActionForward getInfo ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		// if ( isNeedLogin ( request )) {response.sendRedirect ( "login.html" );return mapping.findForward ( "" );}
		
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		String lid = request.getParameter ( "lid" );
		Map map = new HashMap ( );
		map.put ( "opFlag" , "error" );
		if ( getCustId ( request ) != null ) {
			if(request.getParameter ( "dtype" )==null) throw new Exception ("无效类型!");
			CustInfoVo info = new CustInfoVo ( );
			info.setCustid ( getCustId ( request ) );
			info.setDtype ( request.getParameter ( "dtype" ) );
			info.setLid ( new Integer ( lid ) );
			List list = mng_CustService.loadCustMsgByType ( info );
			JSONArray comjson = JSONArray.fromObject ( list.get ( 0 ) );
			String result = comjson.toString ( );
			logger.debug ( "========================key=:" + result );
			response.getWriter ( ).print ( result );
		}else{
			response.getWriter ( ).print ( "" );
		}
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}
	
}